package com.miplus.ccrm.core.service.strategy;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.core.controller.vo.conf.NamesResVo;
import com.miplus.ccrm.core.controller.vo.strategy.RuleVarListReqVo;
import com.miplus.ccrm.core.controller.vo.strategy.RuleVarsListResVo;
import com.miplus.ccrm.core.mapper.SysNamesMapper;
import com.miplus.ccrm.core.pojo.CfgRuleVars;
import com.miplus.ccrm.core.mapper.CfgRuleVarsMapper;
import com.miplus.ccrm.core.service.conf.SysNamesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author wangzl
* @description 针对表【cfg_rule_vars】的数据库操作Service实现
* @createDate 2024-10-21 14:10:00
*/
@Service
public class CfgRuleVarsServiceImpl extends MPJBaseServiceImpl<CfgRuleVarsMapper, CfgRuleVars>
    implements CfgRuleVarsService{
    @Resource
    private SysNamesService sysNamesService;

    @Override
    public IPage<RuleVarsListResVo> getRuleVarPageList(RuleVarListReqVo paras)
    {
        return selectJoinListPage(
                new Page<>(paras.getPageNum(),paras.getPageSize()),
                RuleVarsListResVo.class,
                new MPJLambdaWrapper<CfgRuleVars>()
                        .selectAs("fun_getnames('RULE.VARTYPE',t.var_type)",RuleVarsListResVo::getVarTypeDesc)
                        .selectAs("fun_getnames('RULE.VARGRP',t.var_group)",RuleVarsListResVo::getVarGroupName)
                        .selectAll(CfgRuleVars.class)
                        .in(ObjectUtils.isNotEmpty(paras.getVarGroup()), CfgRuleVars::getVarGroup,paras.getVarGroup())
                        .eq(StringUtils.isNotEmpty(paras.getStatus()),CfgRuleVars::getStatus,paras.getStatus())
                        .like(StringUtils.isNotEmpty(paras.getVarName()),CfgRuleVars::getVarName,paras.getVarName())
                        .eq(StringUtils.isNotEmpty(paras.getTableName()),CfgRuleVars::getTableName,paras.getTableName())
                        .like(StringUtils.isNotEmpty(paras.getFieldName()),CfgRuleVars::getFieldName,paras.getFieldName())
                        .eq(ObjectUtils.isNotNull(paras.getVarId()),CfgRuleVars::getVarId,paras.getVarId())
        );
    }

    /**
     * 只选择有效的字段
     * @param varsGroup
     * @return
     */
    @Override
    public List<RuleVarsListResVo> getRuleVarsList(String varsGroup)
    {
        return selectJoinList(
                RuleVarsListResVo.class,
                new MPJLambdaWrapper<CfgRuleVars>()
                        .selectAs("fun_getnames('RULE.VARTYPE',t.var_type)",RuleVarsListResVo::getVarTypeDesc)
                        .selectAs("fun_getnames('RULE.VARGRP',t.var_group)",RuleVarsListResVo::getVarGroupName)
                        .selectAll(CfgRuleVars.class)
                        .eq(CfgRuleVars::getStatus,"1")
                        .eq(StringUtils.isNotEmpty(varsGroup), CfgRuleVars::getVarGroup,varsGroup)
        );
    }
    @Override
    public RuleVarsListResVo getRuleVar(Long varId)
    {
        return selectJoinOne(
                RuleVarsListResVo.class,
                new MPJLambdaWrapper<CfgRuleVars>()
                        .selectAs("fun_getnames('RULE.VARTYPE',t.var_type)",RuleVarsListResVo::getVarTypeDesc)
                        .selectAs("fun_getnames('RULE.VARGRP',t.var_group)",RuleVarsListResVo::getVarGroupName)
                        .selectAll(CfgRuleVars.class)
                        .eq(CfgRuleVars::getVarId,varId)
        );
    }
    @Override
    public  CmdResVo addRuleVar(String usrId, CfgRuleVars cfgRuleVars)
    {
       if(StringUtils.isEmpty(cfgRuleVars.getVarName())){
           throw new ServiceException(1001,"参数名称不能为空");
       }
       cfgRuleVars.setCreateTime(new Date());
       save(cfgRuleVars);
;       return new CmdResVo(1,cfgRuleVars.getVarId().toString());
    }
    @Override
    public CmdResVo updateRuleVar(String usrId,CfgRuleVars cfgRuleVars)
    {
        if(ObjectUtils.isNull(cfgRuleVars.getVarId()))
        {
            throw new ServiceException(1001,"参数ID不能为空");
        }
        updateById(cfgRuleVars);
        return new CmdResVo(1);
    }
    @Override
    public CmdResVo deleteRuleVar(Long varId)
    {

        removeById(varId);
        return new CmdResVo(1);
    }
    @Override
    public List<NamesResVo> getRuleVarSelectOption(Long varId)
    {
        CfgRuleVars cfgRuleVars = getById(varId);

        if(cfgRuleVars == null )
            return null;
        if( StringUtils.isNotEmpty(cfgRuleVars.getSourceDicId()))
            return sysNamesService.getSysNamesList(cfgRuleVars.getSourceDicId());
        else if( StringUtils.isNotEmpty(cfgRuleVars.getSourceSql()))
            return sysNamesService.getSelectOptionBySql(cfgRuleVars.getSourceSql());
        return null;
    }

}




